<template>
  <el-form :model="formData" ref="form" :rules="ruleForm" label-width="80px">
    <el-form-item v-for="(item, index) in formItems" :key="index" :label="item.label" :prop="item.prop" >
      <el-input v-if="item.type === 'input'" v-model="formData[item.prop]" :placeholder="item.placeholder" :style="{'width': item.width}"> </el-input>
      <el-select v-else-if="item.type === 'select'" v-model="formData[item.prop]" :placeholder="item.placeholder" :style="{'width': item.width}">
        <el-option v-for="(option, optionIndex) in item.options" :key="optionIndex" :label="option.label"
          :value="option.value"></el-option>
      </el-select>
      <!-- 其他表单组件同理 -->
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">提交</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>
 


<script>
export default {
  props: {
    formItems: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      formData: {},
      data1: {},
      data3: {},
      data:{},
      ruleForm: {
        // username:[{ required: true, message: '请输入用户名', trigger: ['blur', 'change'] }]
      }, // 验证规则
    };
  },
  created() {
    this.initRulesFrom()
  },
  mounted() {

  },
  methods: {
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 表单验证通过，提交表单数据
          // 发送请求等操作...
          this.$emit('validstate',valid)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    //初始化校验规则
    initRulesFrom() {
      this.formItems.forEach((item) => {
        this.ruleForm[item.prop] =  item.rules
      })
    }
  }
};
</script>

<style scoped></style>
